<%-- 
    Document   : org
    Created on : 2013-5-2, 13:50:53
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>机构管理</title>
        <script>
            Ext.onReady(function() {
                var orgForm = Ext.create("Ext.form.Panel", {
                    url: "org/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "名称", name: "organization.name", allowBlank: false},
                        {
                            xtype: "combo",
                            fieldLabel: '类别', //子公司，分公司，控股公司，参股公司，其他
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "子公司", value: "子公司"},
                                    {text: "分公司", value: "分公司"},
                                    {text: "控股公司", value: "控股公司"},
                                    {text: "参股公司", value: "参股公司"},
                                    {text: "其他", value: "其他"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "organization.type",
                            value: "子公司"
                        },
                        {fieldLabel: "负责人", name: "organization.manager"},
                        {fieldLabel: "联系方式", name: "organization.contact",regex:Validator.number,regexText:"电话格式不对"},
                        {fieldLabel: "地址", name: "organization.address"},
                        {fieldLabel: "备注", name: "organization.remark"},
                        {name: "organization.id", xtype: "hiddenfield"},
                        {name: "organization.p_id", xtype: "hiddenfield"},
                        {name:"org_tree_id",xtype:"hiddenfield"},
                        {name: "n_type", xtype: "hiddenfield"},
                        {name: "n_leaf", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                orgTree.getStore().load();
                                                orgWin.close();
                                                orgTree.expandAll();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                orgWin.close();
                            }
                        }]
                });
                var orgWin = Ext.create("Ext.window.Window", {
                    title: "机构信息",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [orgForm]
                });
                var depForm = Ext.create("Ext.form.Panel", {
                    url: "dep/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "名称", name: "department.name", allowBlank: false},
                        {fieldLabel: "部门电话", name: "department.phone",regex:Validator.number,regexText:"电话格式不对"},
                        {fieldLabel: "负责人", name: "department.manager"},
                        {fieldLabel: "联系方式", name: "department.contact",regex:Validator.number,regexText:"电话格式不对"},
                        {name: "department.id", xtype: "hiddenfield"},
                        {name:"org_tree_id",xtype:"hiddenfield"},
                        {name: "p_id", xtype: "hiddenfield"},
                        {name: "n_type", xtype: "hiddenfield"},
                        {name: "n_leaf", xtype: "hiddenfield"}

                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                orgTree.getStore().load();
                                                orgTree.expandAll();
                                                depWin.close();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }});
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                depWin.close();
                            }
                        }]
                });
                var depWin = Ext.create("Ext.window.Window", {
                    title: "部门信息",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [depForm]
                });
                var posForm = Ext.create("Ext.form.Panel", {
                    url: "pos/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        column: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "名称", name: "position.name", allowBlank: false},
                        {name: "position.id", xtype: "hiddenfield"},
                        {name: "p_id", xtype: "hiddenfield"},
                        {name:"org_tree_id",xtype:"hiddenfield"},
                        {name: "n_type", xtype: "hiddenfield"},
                        {name: "n_leaf", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                orgTree.getStore().load();
                                                orgTree.expandAll();
                                                posWin.close();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                posWin.close();
                            }
                        }]
                });
                var posWin = Ext.create("Ext.window.Window", {
                    title: "岗位信息",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [posForm]
                });
                var store = Ext.create("Ext.data.TreeStore", {
                    proxy: {
                        type: 'ajax',
                        url: 'orgtree/getTree'
                    },
                    nodeParam: "id",
                    fields: [
                        {name: 'text', type: 'string'},
                        {name: 'type', type: 'string'},
                        {name: 'id', type: 'string'},
                        {name: 'leaf', type: 'boolean'}

                    ],
                    root: {
                        text: '顶级机构',
                        id: 'root',
                        expanded: true
                    }
                });

                var menu = Ext.create('Ext.menu.Menu', {
                    items: [{
                            id: "viewItem",
                            text: "查阅",
                            icon: "resource/images/ico/search.png",
                            handler: function() {
                                var record = orgTree.getSelectionModel().getSelection()[0];
                                var type = record.get("type");
                                var path = "";
                                if (type === "机构") {
                                    path = "org/showDetail/" + record.get("id");
                                } else if (type === "部门") {
                                    path = "dep/showDetail/" + record.get("id");
                                } else if (type === "岗位") {
                                    path = "pos/showDetail/" + record.get("id");
                                }
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "详细信息",
                                    //maximized: true,
                                    width:550,
                                    height:300,
                                    modal:true,
                                    maximizable: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='" + path + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }
                        }, "-", {
                            id: "orgItem",
                            text: '登记机构',
                            icon: "resource/images/ico/add.png",
                            handler: function() {
                                orgForm.getForm().reset();
                                orgForm.getForm().url="org/add";
                                orgForm.getForm().setValues({
                                    "organization.p_id": orgTree.getSelectionModel().getSelection()[0].raw.id,
                                    n_type: "机构",
                                    n_leaf: false
                                });
                                orgWin.show();
                            }}, "-", {
                            id: "depItem",
                            text: '登记部门',
                            icon: "resource/images/ico/add.png",
                            handler: function() {
                                depForm.getForm().reset();
                                depForm.getForm().url="dep/add";
                                depForm.getForm().setValues({
                                    p_id: orgTree.getSelectionModel().getSelection()[0].raw.id,
                                    n_type: "部门",
                                    n_leaf: false
                                });
                                depWin.show();
                            }
                        }, "-", {
                            id: "posItem",
                            text: "登记岗位",
                            icon: "resource/images/ico/add.png",
                            handler: function() {
                                posForm.getForm().reset();
                                posForm.getForm().url="pos/add",
                                posForm.getForm().setValues({
                                    p_id: orgTree.getSelectionModel().getSelection()[0].raw.id,
                                    n_type: "岗位",
                                    n_leaf: true
                                });
                                posWin.show();
                            }
                        }, "-", {
                            id: "updateItem",
                            text: "变更",
                            icon: "resource/images/ico/edit.png",
                            handler: function() {
                                var record = orgTree.getSelectionModel().getSelection()[0];
                                var type = record.get("type");
                                if (type === "机构") {
                                    orgForm.getForm().url="org/update";
                                    orgForm.getForm().setValues({
                                        "organization.name":record.get("text"),
                                        "organization.type":record.get("type"),
                                        "organization.p_id":record.get("p_id"),
                                        "organization.manager":record.get("manager"),
                                        "organization.contact":record.get("contact"),
                                        "organization.address":record.get("address"),
                                        "organization.remark":record.get("remark"),
                                        "org_tree_id":record.get("id")
                                    });
                                    orgWin.show();
                                } else if (type === "部门") {
                                    depForm.getForm().url="dep/update";
                                    depForm.getForm().setValues({
                                        "department.name":record.get("text"),
                                        "department.phone":record.get("phone"),
                                        "department.manager":record.get("manager"),
                                        "department.contact":record.get("contact"),
                                        "org_tree_id":record.get("id")
                                    });
                                    depWin.show();
                                } else if (type === "岗位") {
                                    posForm.getForm().url="pos/update";
                                    posForm.getForm().setValues({
                                        "position.name":record.get("text"),
                                        "org_tree_id":record.get("id")
                                    });
                                    posWin.show();
                                }
                            }
                        }, "-", {
                            id: "deleteItem",
                            text: "删除",
                            icon: "resource/images/ico/remove.png",
                            handler: function() {
                                var record = orgTree.getSelectionModel().getSelection()[0];
                                var type = record.get("type");
                                var path = "";
                                if (type === "机构") {
                                    path = "org/delete" ;
                                } else if (type === "部门") {
                                    path = "dep/delete";
                                } else if (type === "岗位") {
                                    path = "pos/delete";
                                }
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(viewport, {msg: "正在删除..."});
                                        mask.show();
                                        Ext.Ajax.request({
                                            url: path,
                                            params: {
                                                id: record.get("id")
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    var id = orgTree.getSelectionModel().getSelection()[0].get("id");
                                                    orgTree.getStore().getNodeById(id).remove();
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }]
                });
                var orgTree = Ext.create("Ext.tree.TreePanel", {
                    width: 300,
                    store: store,
                    rootVisible: false,
                    tbar: [
                        {
                            text: "展开全部",
                            iconCls: "add",
                            handler: function() {
                                orgTree.expandAll();
                            }
                        }, "-", {
                            text: "收起全部",
                            iconCls: "remove",
                            handler: function() {
                                orgTree.collapseAll();
                            }
                        }
                    ],
                    listeners: {
                        itemcontextmenu: function(view, record, item, index, event) {
                            event.preventDefault();
                            var nodeType = record.raw.type;
                            if (nodeType === "部门") {
                                Ext.getCmp("orgItem").setDisabled(true);
                                Ext.getCmp("depItem").setDisabled(true);
                                Ext.getCmp("posItem").setDisabled(false);
                            } else if (nodeType === "岗位") {
                                Ext.getCmp("orgItem").setDisabled(true);
                                Ext.getCmp("depItem").setDisabled(true);
                                Ext.getCmp("posItem").setDisabled(true);
                            } else {
                                Ext.getCmp("orgItem").setDisabled(false);
                                Ext.getCmp("depItem").setDisabled(false);
                                Ext.getCmp("posItem").setDisabled(false);
                            }
                            menu.showAt(event.getXY());
                        },
                        itemclick: function(view, record) {
                            empGrid.getStore().setProxy({
                                type: "ajax",
                                url: "emp/list",
                                extraParams: {
                                    orgTreeId: record.raw.id,
                                    type: record.raw.type
                                },
                                reader: {
                                    type: "json",
                                    root: "data"
                                }
                            });
                            empGrid.getStore().load();
                        },
                        render: function() {
                            orgTree.expandAll();
                        }
                    }
                });

                var userForm = Ext.create("Ext.form.Panel", {
                    url: "user/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "账号", name: "user.name", allowBlank: false,
                            minLength:6,minLengthText:"用户名至少6位",
                            maxLength:20,maxLengthText:"用户名最多20位"
                        },
                        {fieldLabel: "密码", name: "user.password", id: "password", inputType: "password", allowBlank: false,
                           minLength:6,minLengthText:"用户名至少6位",
                           maxLength:20,maxLengthText:"用户名最多20位"
                        },
                        {
                            xtype: "combo",
                            fieldLabel: '账户类型',
                            labelAlign: "right",
                            editable: false,
                            allowBlank: false,
                            emptyText: "--请选择--",
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "管理员", value: true},
                                    {text: "普通用户", value: false}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "user.admin",
                            value: ""
                        },
                        {name: "user.emp_id", xtype: "hiddenfield"},
                        {name: "user.id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                userWindow.close();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                userWindow.close();
                            }
                        }]
                });
                var userWindow = Ext.create("Ext.window.Window", {
                    title: "用户信息",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [userForm]
                });

                var empForm = Ext.create("Ext.form.Panel", {
                    url: "emp/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "姓名", name: "employee.name", allowBlank: false},
                        {
                            xtype: "combo",
                            fieldLabel: '性别',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "男", value: "男"},
                                    {text: "女", value: "女"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "employee.sex", value: "男"
                        },
                        {fieldLabel: "身份证", name: "employee.id_card", allowBlank: false,regex:Validator.idCard,regexText:"身份证格式不对"},
                        {fieldLabel: "电话", name: "employee.phone", allowBlank: false,regex:Validator.number,regexText:"电话格式不对"},
                        {fieldLabel: "邮件", name: "employee.email", allowBlank: false,regex:Validator.email,regexText:"邮件格式不对"},
                        {
                            xtype: "combo",
                            fieldLabel: '文化程度',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "小学", value: "小学"},
                                    {text: "初中", value: "初中"},
                                    {text: "中专", value: "中专"},
                                    {text: "高中", value: "高中"},
                                    {text: "大专", value: "大专"},
                                    {text: "本科", value: "本科"},
                                    {text: "硕士", value: "硕士"},
                                    {text: "博士", value: "博士"},
                                    {text: "博士后", value: "博士后"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "employee.education",
                            emptyText:"--请选择--",
                            value: ""
                        },
                        {fieldLabel: "家庭住址", name: "employee.address", allowBlank: false},
                        {
                            id: "orgCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "employee.org_id",
                            displayField: 'name',
                            emptyText: "--请选择--",
                            allowBlank: false,
                            valueField: 'id',
                            editable: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {
                                    type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            },
                            listeners: {
                                select: function(combo, record) {
                                    Ext.getCmp("depCombo").getStore().load({url: "dep/getAll/" + record[0].data.id});
                                    //Ext.getCmp("depCombo").setDisabled(false);
                                }
                            }
                        }, {
                            id: "depCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属部门",
                            emptyText: "--请选择--",
                            name: "employee.dep_id",
                            displayField: 'name',
                            allowBlank: false,
                            valueField: 'id',
                            editable: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {type: 'ajax',
                                    url: '',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            },
                            listeners: {
                                select: function(combo, record) {
                                    Ext.getCmp("posCombo").getStore().load({url: "pos/getAll/" + record[0].data.id});
                                    //Ext.getCmp("posCombo").setDisabled(false);
                                },
                                afterrender: function(combo) {
                                    var orgName = Ext.getCmp("orgCombo").getValue();
//                                    if (!orgName || orgName === "") {
//                                        combo.setDisabled(true);
//                                    }
                                }
                            }
                        }, {
                            id: "posCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属岗位",
                            name: "employee.pos_id",
                            displayField: 'name',
                            emptyText: "--请选择--",
                            allowBlank: false,
                            valueField: 'id',
                            editable: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {type: 'ajax',
                                    url: '',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            },
                            listeners: {
                                afterrender: function(combo) {
                                    var depName = Ext.getCmp("depCombo").getValue();
//                                    if (!depName || depName === "") {
//                                        combo.setDisabled(true);
//                                    }
                                }
                            }
                        }, {name: "employee.id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                empWindow.close();
                                                empGrid.getStore().load();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                empWindow.close();
                            }
                        }]
                });
                var empWindow = Ext.create("Ext.window.Window", {
                    title: "职员档案",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [empForm]
                });
                var empStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "name", "age", "sex", "id_card", "phone", "email", "education", "birthday", "address", "org_id", "dep_id", "pos_id"],
                    pageSize: 15,
                    proxy: {
                        type: 'ajax',
                        url: 'emp/list',
                        extraParams: {
                            orgId: "${org_id}"
                        },
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var empGrid = Ext.create("Ext.grid.Panel", {
                    title: "职员档案",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true, columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '机构ID', dataIndex: 'org_id', hidden: true},
                        {header: '部门ID', dataIndex: 'dep_id', hidden: true},
                        {header: '岗位ID', dataIndex: 'pos_id', hidden: true},
                        {header: '姓名', dataIndex: 'name', width: 120},
                        {header: '年龄', dataIndex: 'age', width: 100},
                        {header: '性别', dataIndex: 'sex', width: 100},
                        {header: '身份证', dataIndex: 'id_card', width: 100},
                        {header: '电话', dataIndex: 'phone', width: 100},
                        {header: '邮件', dataIndex: 'email', width: 100},
                        {header: '文化程度', dataIndex: 'education', width: 100},
                        {header: '出生日期', dataIndex: 'birthday', width: 100},
                        {header: '家庭住址', dataIndex: 'address', width: 200}
                    ],
                    store: empStore,
                    tbar: [{xtype: "button", text: "查看", iconCls: "search", handler: function() {
                                var sel = empGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0].data;
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "职员档案",
                                    width: 550,
                                    height: 300,
                                    closeAction: "hide",
                                    layout: "fit", html: "<iframe id='detailFrame' src='emp/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }},
                        {xtype: 'tbseparator'}, {xtype: "button", text: "登记", iconCls: "add",
                            handler: function() {
                                empForm.getForm().reset();
                                empForm.getForm().url = "emp/add";
                                empWindow.show();
                            }},
                        {xtype: 'tbseparator'}, {xtype: "button", text: "变更", iconCls: "edit",
                            handler: function() {
                                var sel = empGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                empForm.getForm().url = "emp/update";
                                empWindow.show();
                                empForm.getForm().setValues({
                                    "employee.id": record.get("id"),
                                    "employee.name": record.get("name"),
                                    "employee.sex": record.get("sex"),
                                    "employee.id_card": record.get("id_card"),
                                    "employee.phone": record.get("phone"),
                                    "employee.email": record.get("email"),
                                    "employee.education": record.get("education"),
                                    "employee.address": record.get("address"),
                                    "employee.org_id": record.get("org_id"),
                                    "employee.dep_id": record.get("dep_id"),
                                    "employee.pos_id": record.get("pos_id")
                                });
                                Ext.getCmp("orgCombo").getStore().load();
                                Ext.getCmp("depCombo").getStore().load({url: "dep/getAll/" + record.get("org_id")});
                                Ext.getCmp("posCombo").getStore().load({url: "pos/getAll/" + record.get("dep_id")});
                            }
                        },
                        {xtype: 'tbseparator'}, {xtype: "button", text: "删除", iconCls: "remove",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(driverGrid, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = driverGrid.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "emp/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    driverGrid.getStore().remove(sel);
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }, "-", {
                            text: "添加用户",
                            iconCls: "add",
                            handler: function() {
                                var sel = empGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                userForm.getForm().reset();
                                Ext.getCmp("password").setDisabled(false);
                                userForm.getForm().url = "user/add";
                                userForm.getForm().setValues({
                                    "user.emp_id": record.get("id")
                                });
                                userWindow.show();
                            }
                        }, "-", {
                            text: "变更用户",
                            iconCls: "edit",
                            handler: function() {
                                var sel = empGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                userForm.getForm().reset();
                                Ext.getCmp("password").setDisabled(true);
                                userForm.getForm().url = "user/update";
                                var tag = true;
                                Ext.Ajax.request({
                                    url: "user/getUserByEmpId",
                                    params: {
                                        empId: record.get("id")
                                    },
                                    success: function(response) {
                                        var json = Ext.JSON.decode(response.responseText);
                                        userForm.getForm().setValues({
                                            "user.emp_id": json.user.emp_id,
                                            "user.id": json.user.id,
                                            "user.name": json.user.name,
                                            "user.admin": json.user.admin
                                        });
                                    },
                                    failure: function(response) {
                                        var json = Ext.JSON.decode(response.responseText);
                                        Ext.Msg.alert("提示", json.msg, function() {
                                            tag = false;
                                        });
                                    }
                                });
                                if (!tag) {
                                    Ext.Msg.alert("提示", "获取账户信息失败！");
                                    return;
                                }
                                userWindow.show();
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: empStore,
                        displayInfo: true
                    })
                });
                var viewport=Ext.create("Ext.Viewport", {
                    layout: "border",
                    items: [
                        {region: "west", title: "机构", layout: "fit", collapsible: true, items: [orgTree]},
                        {region: "center", layout: "fit", items: [empGrid]}
                    ]
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>
